package com.yb.guigu.pick.auth.service.impl;

import com.yb.guigu.pick.auth.bean.vo.request.LoginSuccessVo;
import com.yb.guigu.pick.cache.manager.api.GuiguPickStringCacheManager;
import com.yb.guigu.pick.core.bean.UserCodeExpire;
import com.yb.guigu.pick.core.constant.RedisKeyConstant;
import com.yb.guigu.pick.core.domain.mq.EmailSend;
import com.yb.guigu.pick.core.domain.mq.MobileCodeSend;
import com.yb.guigu.pick.core.util.RandomUtil;
import com.yb.guigu.pick.rabbitmq.messager.api.GuiguPickMessager;
import com.yb.guigu.pick.rabbitmq.messager.api.model.MessageContent;
import com.yb.guigu.pick.rabbitmq.messager.api.model.RabbitMQMessage;
import com.yb.guigu.pick.core.bean.GuiguResult;
import com.yb.guigu.pick.auth.bean.vo.request.LoginRequestVo;
import com.yb.guigu.pick.auth.service.IUserService;
import com.yb.guigu.pick.core.constant.RabbitMQConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/29 12:15
 * FileName: UserServiceImpl
 * Description:
 */

@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    GuiguPickMessager guiguPickMessager;

    @Autowired
    GuiguPickStringCacheManager guiguPickStringCacheManager;


    @Autowired
    UserCodeExpire userCodeExpire;

    @Override
    public GuiguResult login(LoginRequestVo loginRequestVo) {

        if(!StringUtils.hasText(loginRequestVo.getUsername())){
            return GuiguResult.error("用户名不能为空");
        }

        if(!StringUtils.hasText(loginRequestVo.getPassword())){
            return GuiguResult.error("密码不能为空");
        }
        if(!loginRequestVo.getUsername().equals("admin")){
            return GuiguResult.error("用户名错误");
        }
        String replace = UUID.randomUUID().toString().replace("-", "");
        return GuiguResult.success(LoginSuccessVo.builder().username(loginRequestVo.getUsername()).token(replace).build());
    }

    @Override
    public GuiguResult sendEmail(String email) {

        EmailSend emailSend=new EmailSend();

        emailSend.setEmail(email);

        emailSend.setCode(RandomUtil.generator(6));

        String uuid = UUID.randomUUID().toString().replace("-", "");

        emailSend.setUuid(uuid);

        guiguPickStringCacheManager.expire(String.format(RedisKeyConstant.LOGIN_EMAIL_CODE_KEY,email),emailSend.getCode(),userCodeExpire.getEmailCodeTimeout().getSeconds(), TimeUnit.SECONDS);

        RabbitMQMessage build = RabbitMQMessage.builder().content(MessageContent.builder().content(emailSend).msgId(uuid).build()).rabbitId(uuid).exchange(RabbitMQConstant.EmailQueueConstant.EXCHANGE)
                .queueName(RabbitMQConstant.EmailQueueConstant.QUEUE)
                .routingKey(RabbitMQConstant.EmailQueueConstant.ROUTING_KEY).build();

        guiguPickMessager.convertAndSendAsync(build);

        return GuiguResult.success();
    }

    @Override
    public GuiguResult sendMobileCode(String mobile) {

        MobileCodeSend mobileCodeSend=new MobileCodeSend();
        mobileCodeSend.setMobile(mobile);
        mobileCodeSend.setCode(RandomUtil.generateNumber(6));

        String uuid = UUID.randomUUID().toString().replace("-", "");

        mobileCodeSend.setUuid(uuid);

        guiguPickStringCacheManager.expire(String.format(RedisKeyConstant.LOGIN_MOBILE_CODE_KEY,mobile),mobileCodeSend.getCode(),userCodeExpire.getMobileCodeTimeout().getSeconds(), TimeUnit.SECONDS);

        RabbitMQMessage build = RabbitMQMessage.builder().content(MessageContent.builder().content(mobileCodeSend).msgId(uuid).build()).rabbitId(uuid).exchange(RabbitMQConstant.MobileQueueConstant.EXCHANGE)
                .queueName(RabbitMQConstant.MobileQueueConstant.QUEUE)
                .routingKey(RabbitMQConstant.MobileQueueConstant.ROUTING_KEY).build();

        guiguPickMessager.convertAndSendAsync(build);

        return GuiguResult.success();
    }
}
